<template>
	<view class="container" >
		<view class="left-bottom-sign"></view>
		<view class="back-btn" @click="navBack">
			<view class="cuIcon-back_android"></view>
		</view>
		<view class="right-top-sign"></view>
		<!-- 设置白色背景防止软键盘把下部绝对定位元素顶上来盖住输入框等 -->
		<view class="wrapper">
			<view class="left-top-sign">MIOU</view>
			<view class="welcome">
				欢迎来到MIOU！
			</view>
			<view class="input-content">
				<view class="input-item">
					<text class="tit">手机号码</text>
					<input type="number" value="" placeholder="请输入手机号码" maxlength="11" data-key="mobile" @input="inputChange" />
				</view>
				<view class="input-item">
					<text class="tit">验证码</text>
					<view class="flex code-warp">
						<input type="number" value="" placeholder="请输入验证码" maxlength="6" data-key="scode" @input="inputChange" @confirm="toLogin" />
						<button class="scode" type="default" :disabled="!!currSecord" @click="sendCode">{{ state === 'normal' ? '发送验证码' : waitTitle.replace('SECORD', currSecord) }}</button>
					</view>
				</view>
			</view>

			<view class="flex padding">
				<button class="confirm-btn back margin-lr-sm" @click="navBack">残忍拒绝</button>

				<button class="confirm-btn margin-lr-sm" @click="toLogin" :disabled="logining">立即登录</button>
			</view>

		</view>
		<view class="register-section">
			Copyright©2020西安伯牙子期网络科技有限公司版权所有
		</view>
	</view>
</template>

<script>
	import { User } from '@/models/user.js';
	import { mapState, mapActions } from 'vuex'
	let loadingTimer;

	export default {
		data() {
			return {
				mobile: '',
				scode: '',
				logining: false,
				currSecord: 0,
				state: 'normal',
				waitTitle: '稍等（SECORD）秒'
			}
		},
		onLoad() {
			let loginMobile = uni.getStorageSync("loginMobile");
			if(loginMobile) {
				this.$mTool.showLoading('正在重新登录中...')
				User.reLogin(loginMobile).then(info => {
					if(info){
						this.appLogin(info)
						this.jumpHistory()
					}
				});
			}
		},
		methods: {
			...mapActions(['appLogin']),
			inputChange(e) {
				const key = e.currentTarget.dataset.key;
				this[key] = e.detail.value;
			},
			navBack() {
				this.$backPage()
			},
			async sendCode() {
				const { mobile } = this;
				let checkRes = this.$graceChecker.check({
					telnumber: mobile
				}, this.$formRuleConfig.mobileRule)
				if (!checkRes) {
					uni.showToast({
						title: this.$graceChecker.error,
						icon: "none",
					});
					return
				}
				await User.sendCode(mobile).then(() => {
					this.currSecord = 60
					this.state = 'wait';
					this.beginInterval()
				})
			},
			beginInterval() {
				this.currSecord = this.currSecord - 1;
				if (this.currSecord <= 0) {
					this.state = 'normal';
					clearTimeout(loadingTimer)
					loadingTimer = null
				} else {
					loadingTimer = setTimeout(this.beginInterval, 1000);
				}
			},
			async toLogin() {
				const {
					mobile,
					scode
				} = this;
				let checkRes = this.$graceChecker.check({
					telnumber: mobile,
					scode
				}, this.$formRuleConfig.sendCodeRule)
				if (!checkRes) {
					uni.showToast({
						title: this.$graceChecker.error,
						icon: "none",
					});
					return
				}
				this.logining = true;
				await User.login(mobile, scode).then(info => {
					uni.setStorageSync("loginMobile", mobile);
					this.appLogin(info)
					this.jumpHistory()
				}).catch(err => {
					this.logining = false;
				})
			},

			jumpHistory() {
				let beforeUrl = uni.getStorageSync('loginBefore')
				if (!beforeUrl) {
					beforeUrl = '/pages/app/index'
				}
				uni.removeStorage({
					key: 'loginBefore',
					success: function(res) {
						uni.redirectTo({
							url: beforeUrl
						})
					}
				});

			}
		},
		onUnload() {
			if (loadingTimer) {
				clearTimeout(loadingTimer);
				loadingTimer = null;
			}
		},
	}
</script>

<style lang='scss' scoped>
	page {
		background: #fff;
	}

	.container {
		padding-top: 230rpx;
		position: relative;
		width: 100vw;
		height: 100vh;
		overflow: hidden;
		background: #fff;
	}

	.wrapper {
		position: relative;
		z-index: 90;
		background: #fff;
		padding-bottom: 40upx;
	}

	.back-btn {
		position: absolute;
		left: 40upx;
		z-index: 9999;
		padding-top: var(--status-bar-height);
		top: 40upx;
		font-size: 40upx;
		color: $font-color-dark;
	}

	.left-top-sign {
		font-size: 120upx;
		color: $page-color-base;
		position: relative;
		left: -16upx;
	}

	.right-top-sign {
		position: absolute;
		top: 80upx;
		right: -30upx;
		z-index: 95;

		&:before,
		&:after {
			display: block;
			content: "";
			width: 400upx;
			height: 80upx;
			background: #b4f3e2;
		}

		&:before {
			transform: rotate(50deg);
			border-radius: 0 100rpx 0 0;
		}

		&:after {
			position: absolute;
			right: -198upx;
			top: 0;
			transform: rotate(-50deg);
			border-radius: 100rpx 0 0 0;
			/* background: pink; */
		}
	}

	.left-bottom-sign {
		position: absolute;
		left: -270upx;
		bottom: -320upx;
		border: 100upx solid #d0d1fd;
		border-radius: 50%;
		padding: 180upx;
	}

	.welcome {
		position: relative;
		left: 50upx;
		top: -90upx;
		font-size: 46upx;
		color: #555;
		text-shadow: 1px 0px 1px rgba(0, 0, 0, .3);
	}

	.input-content {
		padding: 0 90upx;
	}

	.code-warp {
		display: flex;
		align-items: center;

		.scode {
			height: 60rpx;
			line-height: 60rpx;
			font-size: 24rpx;
			width: 320rpx;
			background: #555;
			color: #fff;

			&[disabled] {
				opacity: 0.6;
			}
		}
	}

	.input-item {
		display: flex;
		flex-direction: column;
		align-items: flex-start;
		justify-content: center;
		/* padding: 0 30upx; */
		border-bottom: 1px solid $page-color-light;
		height: 120upx;
		border-radius: 4px;
		margin-bottom: 50upx;

		&:last-child {
			margin-bottom: 0;
		}

		.tit {
			height: 50upx;
			line-height: 56upx;
			font-size: $font-sm+2upx;
			color: $font-color-base;
		}

		input {
			height: 60upx;
			font-size: $font-base + 2upx;
			color: $font-color-dark;
			width: 100%;
		}
	}

	.confirm-btn {
		width: 630upx;
		height: 76upx;
		line-height: 76upx;
		border-radius: 50px;
		margin-top: 70upx;
		background: #555;
		color: #fff;
		font-size: $font-lg;

		&.back {
			background: #8799a3;
		}

		&:after {
			border-radius: 100px;
		}

		&[disabled] {
			opacity: 0.6;
		}
	}

	.register-section {
		position: absolute;
		left: 0;
		bottom: 50upx;
		width: 100%;
		font-size: $font-sm+2upx;
		color: $font-color-base;
		text-align: center;

		text {
			color: $font-color-spec;
			margin-left: 10upx;
		}
	}
</style>
